package vn.sunnet.backviet01.game.coloa.main;

import java.io.IOException;

import org.anddev.andengine.audio.sound.Sound;
import org.anddev.andengine.audio.sound.SoundFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.modifier.MoveXModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Intent;
import android.view.KeyEvent;

import vn.sunnet.backviet01.game.coloa.constant.Constants;
import vn.sunnet.backviet01.game.coloa.util.MyScene;

/**
 * (c) 2012 QuanLT
 * (c) 2012 Sunnet JSC
 *
 *@author QuanLT
 */

public class Guide extends org.anddev.andengine.ui.activity.BaseGameActivity implements IOnSceneTouchListener{
	
	// ===========================================================
	// Constants
	// ===========================================================
	
	// ===========================================================
	// Fields
	// ===========================================================
	private Camera mCamera;
	private MyScene mScene;
	
	private TextureRegion[] mGuideRegions;
	private TextureRegion mNextRegion;
	
	private long mTimeUpdate;
	private int mCountNext = 0;
	
	private Sprite[] mGuides;
	private Sprite mNextButton;
	
	private Sound mLaughing;
	
	boolean isNext = false;
	
	private static final int[] TIME = {1000, 1500, 1200, 1000, 600};
	
	// ===========================================================
	// Constructor
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public Engine onLoadEngine() {
		mCamera = new Camera(0, 0, Constants.CAMERA_WIDTH, Constants.CAMERA_HEIGHT);
		
		EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), mCamera);

		eo.getRenderOptions().disableExtensionVertexBufferObjects();
		eo.getTouchOptions().setRunOnUpdateThread(true);
		eo.setNeedsSound(true);
		
		return mEngine = new Engine(eo);
	}

	@Override
	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		
		mGuideRegions = new TextureRegion[5];
		
		BitmapTextureAtlas guide1 = new BitmapTextureAtlas(1024, 512, TextureOptions.NEAREST);
		BitmapTextureAtlas guide2 = new BitmapTextureAtlas(1024, 512, TextureOptions.NEAREST);
		BitmapTextureAtlas guide3 = new BitmapTextureAtlas(1024, 512, TextureOptions.NEAREST);
		BitmapTextureAtlas guide4 = new BitmapTextureAtlas(1024, 512, TextureOptions.NEAREST);
		BitmapTextureAtlas guide5 = new BitmapTextureAtlas(1024, 512, TextureOptions.NEAREST);
		
		mGuideRegions[0] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(guide1, this, "guide1.png", 0, 0);
		mGuideRegions[1] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(guide2, this, "guide2.png", 0, 0);
		mGuideRegions[2] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(guide3, this, "guide3.png", 0, 0);
		mGuideRegions[3] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(guide4, this, "guide4.png", 0, 0);
		mGuideRegions[4] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(guide5, this, "guide5.png", 0, 0);
		
		BitmapTextureAtlas next = new BitmapTextureAtlas(64, 64);
		mNextRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(next, this, "quit.png", 0, 0);
		
		this.mEngine.getTextureManager().loadTextures(guide1, guide2, guide3, guide4, guide5);
		this.mEngine.getTextureManager().loadTextures(next);
		
		SoundFactory.setAssetBasePath("mfx/");
		try {
			mLaughing = SoundFactory.createSoundFromAsset(getSoundManager(), this, "laughing.ogg");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void onLoadComplete() {
		mTimeUpdate = System.currentTimeMillis();
	}
	
	@Override
	public Scene onLoadScene() {
		mScene  = new MyScene();
		
		mGuides = new Sprite[5];
		
		for(int i = 0; i < 5 ; i++) {
			mGuides[i] = new Sprite(0, 0, mGuideRegions[i]);
			mGuides[i].setZIndex(5 - i);
			mScene.attachChild(mGuides[i]);
		}
		
		mNextButton = new Sprite(730, 420, mNextRegion);
		mScene.attachChild(mNextButton);
		mNextButton.setZIndex(5);
		
		mScene.sortChildren();
		mScene.registerUpdateHandler(mGuideUpdate);

		mScene.setOnSceneTouchListener(this);
		mScene.setTouchAreaBindingEnabled(true);
		mScene.setOnSceneTouchListenerBindingEnabled(true);
		
		return mScene;
	}

	@Override
	public boolean onSceneTouchEvent(Scene arg0, TouchEvent arg1) {
		if (!isNext) {
			isNext = true;
			mScene.clearUpdateHandlers();
			Intent i = new Intent(Guide.this, MainGame.class);
			startActivity(i);
			finish();
		}
		return true;
	}
	
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
		if (pKeyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(pKeyCode, pEvent);
	}
	// ===========================================================
	// Methods
	// ===========================================================

	private IUpdateHandler mGuideUpdate = new IUpdateHandler() {

		@Override
		public void onUpdate(float arg0) {
			long time = System.currentTimeMillis() - mTimeUpdate;
			if (time >  TIME[mCountNext]) {
				if (mCountNext == 2) {
					mLaughing.play();
				}
				if (mCountNext == 4) {
					mLaughing.stop();
					mScene.clearUpdateHandlers();
					if (!isNext) {
						Intent i = new Intent(Guide.this, MainGame.class);
						startActivity(i);
						finish();
					}
				} else {
					mGuides[mCountNext].registerEntityModifier(new MoveXModifier(0.6f, 0, -800));
					mGuides[mCountNext + 1].registerEntityModifier(new MoveXModifier(0.6f, 800, 0));
										
				}
				mTimeUpdate = System.currentTimeMillis();
				mCountNext++;
			}
		}

		@Override
		public void reset() {
			
		}
		
	};
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================

}


